﻿/*
Commerce by Onex – eCommerce software platform (www.ecommerce.byonex.com)
Copyright (C) 2008-2010 by Onex Group

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 
 */
using System;
using System.Collections.Generic;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using ShopModel;
using ShopBLL;
using System.Resources;
using CrystalDecisions.CrystalReports.Engine;
using System.Data;
using System.Collections;


public partial class ClientOrders : BasePage
{
    #region CONSTANTS

    private const int PAYMENT_COLUMN_INDEX = 5;
    private const int REALIZATION_END_DATE_COLUMN_INDEX = 2;
    private const int INVOICE_COLUMN_INDEX = 7;
    private const int EDIT_COLUMN_INDEX = 9;

    #endregion CONSTANTS

    #region VARIABLES

    private bool isShopService = false;

    #endregion VARIABLES

    #region EVENTS

    protected void gvOrders_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            Realization realization = (Realization)e.Row.DataItem;
            e.Row.Cells[PAYMENT_COLUMN_INDEX].Text = CommonTools.GetPaymentString(realization.PaymentID);

            if (realization.EndDate == DateTime.MinValue)
            {
                e.Row.Cells[REALIZATION_END_DATE_COLUMN_INDEX].Text = string.Empty;
            }
        }

    }

    protected void gvOrders_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
    {
        gvOrders.SelectedIndex = e.NewSelectedIndex;

        gvProducts.DataSource = OrderManager.Instance.GetRealizationOrderedProductsList(int.Parse(gvOrders.Rows[e.NewSelectedIndex].Cells[0].Text), ShopBLL.CommonTools.CurrentLanguageID);
        gvProducts.DataBind();

        lblTitle.Visible = true;
    }

    protected void gvOrders_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        gvOrders.PageIndex = e.NewPageIndex;
        gvOrders.DataBind();
    }

    #endregion EVENTS

    #region PAGE EVENTS

    protected void Page_PreInit(object sender, EventArgs e)
    {
        if (this.Page.User.Identity.IsAuthenticated && (Page.User.IsInRole("Administrator") || Page.User.IsInRole("ShopManager") || Page.User.IsInRole("ShopAssistant")))
        {
            this.MasterPageFile = "cms.master";
            this.Title = Resources.Language.CmsUserOrders;
            isShopService = true;
        }
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        MembershipUser user = null;
        Customer customer = null;
        if (Request.QueryString["id"] != null && isShopService)
        {
            user = Membership.GetUser(new Guid(Request.QueryString["id"]));
            customer = OrderManager.Instance.GetCustomerByUserId(new Guid(Request.QueryString["id"]), CommonTools.CurrentLanguageID);
            gvOrders.Columns[EDIT_COLUMN_INDEX].Visible = true;
            lblHeader.Text = string.Format("{0} - {1} {2}", Resources.Language.Orders, customer.FirstName, customer.Surname);
        }
        else
        {
            user = Membership.GetUser(true);
            gvOrders.Columns[EDIT_COLUMN_INDEX].Visible = false;
        }

        gvOrders.DataSource = OrderManager.Instance.GetRealizationListForCustomer((Guid)user.ProviderUserKey, rbl.SelectedValue == "1");
        gvOrders.DataBind();

        lblTitle.Visible = false;
        gvProducts.DataSource = null;
        gvProducts.DataBind();
    }

    #endregion PAGE EVENTS
}
